#!/bin/sh

#
# This script is used to clean slave host from running spies and old file
#

#
# Arguments : 1 - a file which contain host names
#      (each line like "[ ]*name[;.*]")
#
#             2 - a folder to clean
#
# STDOUT = not specified
#
#
# RETCODE :
#     0   no errors
#     1   one (at least) argument rejected
#
#

# GLOBAL

ERR_ARG=$((1))

##


HOSTLIST=$1
FOLD=$2


#CMD0="touch /tmp/AAA"
# TODO refine kill
CMD1="killall -9 java"
CMD2="rm -rf "${FOLD}""


if ! test -f "${HOSTLIST}"
then
echo -e "USAGE : $0 list-filename fold\n\t Trying $0 $*"
exit ${ERR_ARG}
fi


# don't clean /, so at least three characters
if ! (echo "${FOLD}" | grep '...')
then
echo -e "USAGE : $0 list-filename fold(not /)\n\t Trying $0 $*"
exit ${ERR_ARG}
fi


IFS=$'\n'
for i in $(cat ${HOSTLIST})
do
  HOST=$(echo $i | sed 's/;.*$//')
  echo "<$HOST>"
  #ssh "$HOST" ${CMD0}
  ssh "$HOST" ${CMD1}
  ssh "$HOST" ${CMD2}
done
